<?php

class AjaxAction extends BaseAction {

	public function _initialize(){
        parent::_initialize();
    }

    /**
     * 上传图片
     */
    public function uploadImg(){
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();// 实例化上传类
        $upload->allowExts =    array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =    './Upload/'.date('Y-m-d').'/'; // 设置附件上传根目录
        //上传文件
        $info =  $upload->upload();
        if(!$info) {    // 上传错误提示错误信息
            $data['status']  = 0 ;
            $data['msg'] = $upload->getErrorMsg();
        }else{      // 上传成功
            $info = $upload->getUploadFileInfo();
            $one = current($info);
            $data['status'] = 1;
            $data['info'] = '/Upload/'.date('Y-m-d').'/'.$one['savename'];
        }

        echo json_encode($data);
        exit;
    }

    /**
     * 获取文件大小
     */
    public function getFileSize() {
        // 初始化返回结果
        $result = array(
            'status' => 1,
            'size' => 0
        );

        $filename = $_GET['filename'];
        $filepath = './Video/' . $filename;

        $result['size'] = file_exists($filepath) ? filesize($filepath) : 0;

        echo json_encode($result);
    }



    /**
     * 上传视频
     */
    public function uploadVideo() {

        $filename = $_POST['name'];

        file_put_contents('./Video/'.$filename, file_get_contents ( $_FILES["file"]["tmp_name"]),  FILE_APPEND);

        $result = array('succ' => 1);
        echo json_encode($result);

    }

    /**
     * 上传完成操作
     */
    public function uploadComplete() {
        $filename = $_GET['filename'];
        $filepath = '/Video/' . $filename;
        // $data['path'] = $filepath;
        // $res = D('video')->add($data);
        // $info = array();
        // if ($res) {
        $info['status'] = 1;
        $info['path'] = $filepath;
        // } else {
            // $info['status'] = 0;
        // }
        echo json_encode($info);
    }


    public function delColumn(){
        $info = I('post.data');
        $cidarray = explode(',',$info);
        foreach($cidarray as $k => $v){
            $ids = D('Column')->getDelcolumn($v);
            $map['cid'] = array('in',$ids);
            D('Column')->where($map)->delete();
        }
        $data['status'] = 1;
        $data['msg'] = '删除成功！';
        echo json_encode($data);
    }

     public function ColumnSort(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);

        foreach($arr as  $k => $v){
            $sort = explode('-',$v);
            if(is_numeric($sort[1])){
                $map['cid']   = $sort[0];
                $data['sort'] = $sort[1];
                $save = D('Column')->where($map)->save($data);
            }
        }
        $callback = array('status'=>1,'msg'=>'更新排序成功！');
        echo json_encode($callback);
        return ;
    }

    public function delarclist(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);	// 用回调函数过滤数组中的值
        $del  = D('Arclist')->delmorelist($arr);
        if($del){
            $callback = array('status'=>1,'msg'=>'删除成功！');
        }else{
            $callback = array('status'=>0,'msg'=>'删除失败！');
        }
        echo json_encode($callback);
        return ;
    }

		// 删除视频信息
		public function delVideoInfo()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);	// 用回调函数过滤数组中的值
			$res = true;
			foreach ($arr as $key => $value) {
				$id = $value;
				$map = array('id' => $id);
				$del = D('video')->where($map)->delete();
				if(!$del){
					$res = false;
					break;
				}
			}
			if ($res) {
				$callback = array('status'=>1,'msg'=>'删除成功！');
			} else {
				$callback = array('status'=>0,'msg'=>'删除失败！');
			}
			echo json_encode($callback);
			return;
		}

    public function delLink(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);
        $map['lid'] = array('in',$arr);
        $del  = D('links')->where($map)->delete();
        if($del){
            $callback = array('status'=>1,'msg'=>'删除成功！');
        }else{
            $callback = array('status'=>0,'msg'=>'删除失败！');
        }
        echo json_encode($callback);
        return ;
    }

    public function delMessage(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);
        $map['mid'] = array('in',$arr);
        $del  = D('message')->where($map)->delete();
        if($del){
            $callback = array('status'=>1,'msg'=>'删除成功！');
        }else{
            $callback = array('status'=>0,'msg'=>'删除失败！');
        }
        echo json_encode($callback);
        return ;
    }


    public function setsession(){
        $name = I('post.sname');
        $_SESSION[$name] = $_POST['info'];
        echo 1;
    }

    public function password(){
        $password = md5(I('post.password'));
        $map['uid'] = 1;
        $info = D('admin')->where($map)->find();
        if( $password == $info['password']){
            echo 1;
        }else{
            echo 0;
        }
    }

    public function ArclistSort(){
        $info = I('post.data');
        $arr  = explode(',', $info);
        $arr  = array_filter($arr);
        foreach($arr as  $k => $v){
            $sort = explode('-',$v);
            if(is_numeric($sort[1])){
                $map['id']   = $sort[0];
                $data['sort'] = $sort[1];
                $save = D('arclist')->where($map)->save($data);
            }
        }
        $callback = array('status'=>1,'msg'=>'更新排序成功！');
        echo json_encode($callback);
        return ;
    }

		// 招聘城市排序更新
		public function recruitCitySort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('recruit_city')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		// 招聘类别排序更新
		public function recruitCategorySort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('recruit_category')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		// 视频排序
		public function videoSort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('video')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		// 游戏排序
		public function gameSort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('game')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		// 删除游戏信息
		public function delGameInfo()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);	// 用回调函数过滤数组中的值
			$res = true;
			foreach ($arr as $key => $value) {
				$id = $value;
				$map = array('id' => $id);
				$del = D('game')->where($map)->delete();
				if(!$del){
					$res = false;
					break;
				}
			}
			if ($res) {
				$callback = array('status'=>1,'msg'=>'删除成功！');
			} else {
				$callback = array('status'=>0,'msg'=>'删除失败！');
			}
			echo json_encode($callback);
			return;
		}

		// 疑难问题排序
		public function answerSort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('answer')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		public function recruitSort()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);
			foreach($arr as  $k => $v){
					$sort = explode('-',$v);
					if(is_numeric($sort[1])){
							$map['id']   = $sort[0];
							$data['sort'] = $sort[1];
							$save = D('recruit')->where($map)->save($data);
					}
			}
			$callback = array('status'=>1,'msg'=>'更新排序成功！');
			echo json_encode($callback);
			return ;
		}

		// 删除疑难问题
		public function delAnswerInfo()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);	// 用回调函数过滤数组中的值
			$res = true;
			foreach ($arr as $key => $value) {
				$id = $value;
				$map = array('id' => $id);
				$del = D('answer')->where($map)->delete();
				if(!$del){
					$res = false;
					break;
				}
			}
			if ($res) {
				$callback = array('status'=>1,'msg'=>'删除成功！');
			} else {
				$callback = array('status'=>0,'msg'=>'删除失败！');
			}
			echo json_encode($callback);
			return;
		}

		public function delRecruit()
		{
			$info = I('post.data');
			$arr  = explode(',', $info);
			$arr  = array_filter($arr);	// 用回调函数过滤数组中的值
			$res = true;
			foreach ($arr as $key => $value) {
				$id = $value;
				$map = array('id' => $id);
				$del = D('recruit')->where($map)->delete();
				if(!$del){
					$res = false;
					break;
				}
			}
			if ($res) {
				$callback = array('status'=>1,'msg'=>'删除成功！');
			} else {
				$callback = array('status'=>0,'msg'=>'删除失败！');
			}
			echo json_encode($callback);
			return;
		}


}
